package com.sz.biz.logistics.core.service.impl;

import com.google.common.collect.Lists;
import com.sz.biz.common.constants.ModuleNames;
import com.sz.biz.logistics.core.entity.ProductCargotypeRel;
import com.sz.biz.logistics.core.service.ProductCargoTypeRelService;
import com.sz.biz.logistics.core.service.ProductService;
import com.sz.biz.logistics.core.dto.ProductCargotypeRelDto;
import com.sz.common.base.constants.UserActions;
import com.sz.common.base.dao.ParamData;
import com.sz.common.base.service.AbstractService;
import com.sz.common.base.utils.CollectionUtils;
import com.sz.common.core.service.PrincipalLogUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Function:  ProductCargoTypeRelService <br>
 * Author: penghui.zheng <br>
 * Date: 2017-05-9 9:58:00
 */
@Service
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class ProductCargoTypeRelServiceImpl extends AbstractService implements ProductCargoTypeRelService {

    @Autowired
    private ProductService productService;

    /**
     * 新增
     *
     * @param entry 新增的实体
     * @return 新增是否成功
     */
    @Override
    public int save(ProductCargotypeRel entry) {
        dao.save(getSqlName("insert"), entry);
        //保存操作日志
        Map<String, Object> dataBackup = new HashMap<>();
        dataBackup.put("ADD_PRODUCT_CARGO_TYPE", entry);
        PrincipalLogUtils.addOperationLog(ModuleNames.BIZ,
                ModuleNames.BIZ_PRODUCT_CARGO_TYPE,
                UserActions.ADD,
                "添加产品货物类型",
                dataBackup);

        return entry.getId();
    }

    /**
     * 批量新增
     *
     * @param list
     */
    @Override
    public void batchsave(List<ProductCargotypeRel> list) {
        dao.batchInsert(getSqlName("insert"), list);
        //保存操作日志
        Map<String, Object> dataBackup = new HashMap<>();
        dataBackup.put("ADD_PRODUCT_CARGO_TYPE", list);
        PrincipalLogUtils.addOperationLog(ModuleNames.BIZ,
                ModuleNames.BIZ_PRODUCT_CARGO_TYPE,
                UserActions.ADD,
                "添加产品货物类型",
                dataBackup);
    }

    /**
     * 订单下单产品列表接口
     * @param productId
     * @return 查询出来的实体对象
     */
    @Override
    public List<ProductCargotypeRelDto> findByProductIdForOrder(int productId) {
        ParamData pd = new ParamData();
        //获取所有的产品id
        List<Integer> ids = productService.findIds(productId);
        if(null == ids || ids.size() <= 0){
            ids = new ArrayList<>();
            ids.add(-1);
        }
        pd.put("productIds", ids);
        return dao.findForList(getSqlName("findByProductIdForOrder"), pd, ProductCargotypeRelDto.class);
    }

    /**
     * 产品列表
     * @param productId
     * @return 查询出来的实体对象
     */
    @Override
    public List<ProductCargotypeRelDto> findByProductId(int productId) {
        List<Integer> ids = Lists.newArrayList();
        ids.add(productId);
        return listByProductIds(ids);
    }

    /**
     * 产品货物类型列表
     * @param productIds
     * @return 查询出来的实体对象
     */
    @Override
    public List<ProductCargotypeRelDto> findByProductIds(List<Integer> productIds){
        return listByProductIds(productIds);
    }

    /**
     * 根据产品id获取货物类型
     * @param productIds
     * @return
     */
    private List<ProductCargotypeRelDto> listByProductIds(List<Integer> productIds){
        ParamData pd = new ParamData();
        if(CollectionUtils.isEmpty(productIds)){
            productIds.add(-1);
        }
        pd.put("productIds", productIds);
        return dao.findForList(getSqlName("findByProductId"), pd, ProductCargotypeRelDto.class);
    }

    /**
     * 根据product uid获取产品可运送货物类型
     * @param productUid
     * @return
     */
    @Override
    public List<String> findByProductUid(String productUid){
        ParamData pd = new ParamData();
        pd.put("productUid", productUid);
        return dao.findForList(getSqlName("findByProductUid"), pd, String.class);
    }

    /**
     * 根据主键Id查询单个实体
     *
     * @param id 主键id
     * @return 查询出来的实体对象
     */
    @Override
    public ProductCargotypeRel findById(Integer id) {
        return (ProductCargotypeRel) dao.findForObject(getSqlName("findById"), id);
    }

    /**
     * 根据productId删除
     *
     * @param productId productId
     */
    @Override
    public void deleteByProductId(Integer productId) {
        dao.delete(getSqlName("deleteByProductId"), productId);
        //保存操作日志
        Map<String, Object> dataBackup = new HashMap<>();
        dataBackup.put("DELETE_PRODUCT_CARGO_TYPE", productId);
        PrincipalLogUtils.addOperationLog(ModuleNames.BIZ,
                ModuleNames.BIZ_PRODUCT_CARGO_TYPE,
                UserActions.DELETE, "删除产品货物类型", dataBackup);
    }

    @Override
    protected String getMapperNamespace() {
        return "ProductCargotypeRelMapper";
    }
}
